Imports System.Data.SqlClient

Public Class cls_modulohabilitado_

    Implements ICloneable

#Region "Variables ..."
    Private int_idmodulohabilitado As Integer = 0
    Private int_idgrupo As Integer = 0
    Private str_idmodulo As String = ""

    Private col_Items As Collections.Generic.List(Of cls_modulohabilitado_) = Nothing
#End Region

#Region "Propiedades ..."
    ReadOnly Property This
        Get
            Return Me
        End Get
    End Property
    ReadOnly Property ThisDescripcion As String
        Get
            Return Me.ToString
        End Get
    End Property
    ReadOnly Property pKey
        Get
            Return idmodulohabilitado
        End Get
    End Property
    Property idmodulohabilitado() As Integer
        Get
            idmodulohabilitado = int_idmodulohabilitado
        End Get
        Set(ByVal value As Integer)
            int_idmodulohabilitado = value
        End Set
    End Property
    Property idgrupo() As Integer
        Get
            idgrupo = int_idgrupo
        End Get
        Set(ByVal value As Integer)
            int_idgrupo = value
        End Set
    End Property
    Property idmodulo() As String
        Get
            idmodulo = str_idmodulo
        End Get
        Set(ByVal value As String)
            str_idmodulo = value
        End Set
    End Property

    Property Items() As Collections.Generic.List(Of cls_modulohabilitado_)
        Get
            Items = col_Items
        End Get
        Set(ByVal value As Collections.Generic.List(Of cls_modulohabilitado_))
            col_Items = value
        End Set
    End Property
#End Region

    Private Sub CargarColeccion(ByVal obj_Reader As SqlDataReader)
        col_Items = New Collections.Generic.List(Of cls_modulohabilitado_)
        While obj_Reader.Read()
            Dim Elemento As New cls_modulohabilitado_
            Elemento.int_idmodulohabilitado = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idmodulohabilitado")))

            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idgrupo"))) = False Then
                Elemento.int_idgrupo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idgrupo")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idmodulo"))) = False Then
                Elemento.str_idmodulo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idmodulo")))
            End If
            col_Items.Add(Elemento)
        End While
    End Sub

    Private Sub CargarVariables(ByVal obj_Reader As SqlDataReader)
        obj_Reader.Read()
        int_idmodulohabilitado = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idmodulohabilitado")))

        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idgrupo"))) = False Then
            int_idgrupo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idgrupo")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idmodulo"))) = False Then
            str_idmodulo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idmodulo")))
        End If
    End Sub

    Public Function Nuevo(Optional ByVal ObtenerID As Boolean = False) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "modulohabilitado__Nuevo"
                .Parameters.Add("@idgrupo", SqlDbType.Int).Value = int_idgrupo
                .Parameters.Add("@idmodulo", SqlDbType.NVarChar).Value = str_idmodulo
                .ExecuteNonQuery()
            End With
            If ObtenerID Then
                CargarUltimo()
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al Crear el registro de modulohabilitado_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Function

    Public Sub Modificar()
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "modulohabilitado__Modificar"
                .Parameters.Add("@idmodulohabilitado", SqlDbType.Int).Value = int_idmodulohabilitado
                .Parameters.Add("@idgrupo", SqlDbType.Int).Value = int_idgrupo
                .Parameters.Add("@idmodulo", SqlDbType.NVarChar).Value = str_idmodulo
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al Modificar el registro de modulohabilitado_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Sub Eliminar(ByVal idmodulohabilitado As Integer)
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "modulohabilitado__Eliminar"
                .Parameters.Add("@idmodulohabilitado", SqlDbType.Int).Value = idmodulohabilitado
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al Eliminar el registro de modulohabilitado_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Function Buscar(ByVal idmodulohabilitado As Integer) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            Buscar = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "modulohabilitado__Buscar"
                .Parameters.Add("@idmodulohabilitado", SqlDbType.Int).Value = idmodulohabilitado
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
                Buscar = True
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al Buscar el registro de modulohabilitado_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Sub CargarUltimo()
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "modulohabilitado__CargarUltimo"
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al CargarUltimo registro de modulohabilitado_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Sub

    Public Function CargarTodos() As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            CargarTodos = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "modulohabilitado__CargarTodos"
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarColeccion(obj_Reader)
                CargarTodos = True
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al CargarTodos los registros de modulohabilitado_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Function BuscarXidmoduloHabilitadoXidgrupo(ByVal idmodulo As String, ByVal idgrupo As Integer) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            BuscarXidmoduloHabilitadoXidgrupo = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "modulohabilitado__BuscarXidmoduloXidgrupo"
                .Parameters.Add("@idmodulo", SqlDbType.NVarChar).Value = idmodulo
                .Parameters.Add("@idgrupo", SqlDbType.Int).Value = idgrupo
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
                BuscarXidmoduloHabilitadoXidgrupo = True
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al BuscarXidmoduloHabilitadoXidgrupo el registro de modulohabilitado_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Function Clone() As Object Implements System.ICloneable.Clone
        Dim obj_modulohabilitado_Clon As New cls_modulohabilitado_
        obj_modulohabilitado_Clon.int_idmodulohabilitado = Me.int_idmodulohabilitado
        obj_modulohabilitado_Clon.int_idgrupo = Me.int_idgrupo
        obj_modulohabilitado_Clon.str_idmodulo = Me.str_idmodulo
        obj_modulohabilitado_Clon.col_Items = Me.col_Items
        Return obj_modulohabilitado_Clon
    End Function

End Class
